package com.graduationproject.examonline.master.service;

import com.graduationproject.examonline.master.entity.Master;
import com.graduationproject.examonline.master.mapper.MasterMapper;
import com.graduationproject.examonline.utils.MD5Utils;
import com.graduationproject.examonline.utils.Result;
import com.graduationproject.examonline.utils.SingelKey;
import com.graduationproject.examonline.utils.Token;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MasterService {

    @Autowired
    private MasterMapper masterMapper;

    public Result login(String masterId, String masterPassword) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            String masterPasswordMD5 = MD5Utils.stringToMD5(masterPassword);
            params.put("masterId", masterId);
            params.put("masterPassword", masterPasswordMD5);
            System.out.println(masterId);
            List<Master> record = masterMapper.findMasterByIdAndPassword(params);
            if (record.size() > 0) {
                Map<String, String> info = new HashMap<String, String>();
                info.put("userId", masterId);
                info.put("userName", masterId);
                info.put("identity", "master");

                long overDate = new Date().getTime() + 1 * 60 * 60 * 1000;
                String token = Token.createToken(masterId, "master", masterId, new Date(overDate), SingelKey.getInstance().getKey());
                return new Result(0, 200, "登录成功", token, info);
            }
            return new Result(1, 200, "登录失败，密码错误");
        } catch (MyBatisSystemException e) {
            e.printStackTrace();
            return new Result(1, 200, "登录失败，检查身份是否正确");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 200, "登录失败，服务器出错");
        }
    }

    public Result getUserInfo(String master_id) {
        try {
            List<Master> list = masterMapper.getUserInfoById(master_id);
            if (list.size() > 0) {
                return new Result(0, 200, list.get(0));
            }
            return new Result(1, 200, "nothing");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 500, "error");
        }
    }

    public Result updateName(Map<String, Object> params) {
        try {
            masterMapper.updateMasterById(params);
            return new Result(0, 200, "ok");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 500, "error");
        }
    }

    public Result updatePassword(Map<String, Object> params) {
        try {
            Map<String, Object> params2 = new HashMap<String, Object>();
            String masterPasswordMD5 = MD5Utils.stringToMD5((String) params.get("pre_password"));
            String nextMasterPasswordMD5 = MD5Utils.stringToMD5((String) params.get("next_master_password"));
            params2.put("masterId", params.get("master_id"));
            params2.put("masterPassword", masterPasswordMD5);
            List<Master> record = masterMapper.findMasterByIdAndPassword(params2);
            if (record.size() > 0) {
                params.put("next_master_password", nextMasterPasswordMD5);
                masterMapper.updateMasterById(params);
                return new Result(0, 200, "ok");
            } else {
                return new Result(1, 200, "密码错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 500, "error");
        }
    }

    public Result update(Map<String, Object> params) {
        try {
            String token = (String) params.get("token");
            String master_id = (String) params.get("master_id");
            String next_master_id = (String) params.get("next_master_id");
            String next_master_password = (String) params.get("next_master_password");

            if (Token.parseToken(token, SingelKey.getInstance().getKey()) != null) {
                Map<String, Object> nextParams = new HashMap<String, Object>();
                nextParams.put("master_id", master_id);
                nextParams.put("next_master_id", next_master_id);
                nextParams.put("next_master_password", next_master_password);
                masterMapper.updateMasterById(params);
                return new Result(0, 200, "操作成功");
            } else {
                return new Result(2, 200, "身份认证失败，请重新登录");
            }

        } catch (Exception e) {
            System.out.println(e);
            return new Result(1, 501, "操作失败，服务器出错了");
        }
    }
}
